//
//  CZStatusModel.swift
//  sinaWeiboswift
//
//  Created by 段文杰 on 16/4/13.
//  Copyright © 2016年 段文杰. All rights reserved.
//

import UIKit

class CZStatusModel: NSObject {
    
    /// 微博创建时间
    var created_at: String?
    
    /// 微博ID
    var id:	Int64 = 0
    
    /// 微博信息内容
    var text: String?
    
    /// 微博来源
    var source: String?
    
    /// 微博的配图 [Int], [String: AnyObject], [[String: AnyObject]]
    /// 数组,里面保存的是字典
    var pic_urls:[[String: AnyObject]]?{
    
        didSet{
        
//            HMPrint(pic_urls)
            let count = pic_urls?.count ?? 0
            
            
            if count == 0 {
//                HMPrint("没有图片")
                return
            }
            storePicutreURLs = [NSURL]()
            
            //有图片 进行字典转模型
            for dict in pic_urls! {
                let urlString = dict["thumbnail_pic"] as! String
                
                let URL = NSURL(string: urlString)
                
                storePicutreURLs?.append(URL!)
                
            }
            
        
        
        
        }
    
    
    
    
    }
    //转发微博
    var retweeted_status: CZStatusModel?
    //微博大图
    var storePicutreURLs: [NSURL]?

    var user : CZUser?
    /// 字典转模型
    init(dict: [String: AnyObject]) {
        super.init()
        
        setValuesForKeysWithDictionary(dict)
    }
    //判断user值 双重嵌套 字典转模型再来一次
    override func setValue(value: AnyObject?, forKey key: String) {
        if key == "pic_urls" {
//            HMPrint("\(value)")
        }
        
        
        
        if key == "user" {
            if let userdict = value as? [String: AnyObject]{
            //有值
                user = CZUser(dict: userdict)
            }
            return
            
        }
        
        
        //判断retweeted_status有没有值

        if key == "retweeted_status", let retweeted_statusdict = value as? [String: AnyObject] {
            //有值
            retweeted_status = CZStatusModel(dict: retweeted_statusdict)
//            HMPrint(retweeted_status)
            return
        }
        super.setValue(value, forKey: key)
    }
    
    /// 字典中的key在模型中找不到对应的属性
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    /// 对象的打印
    override var description: String {
        get {
            let keys = ["created_at", "id", "text", "source", "pic_urls","retweeted_status","user"]
            // 通过key在模型中招属性的值,找到属性的值,将key和值,拼接到字典里面
            return self.dictionaryWithValuesForKeys(keys).description
        }
    }
}

